import {
  ds
} from './data'

const msgObj = {
  request(cb) { // 允许接收通知
    wx.requestSubscribeMessage({
      tmplIds: ['Yng-O0DKbUJWV7C4hM4Hbmrnk8tE_goB09wTNFWscrg'],
      success(res) {
        if (cb) cb(res)
      }
    })
  },
  /**
   * 发送微信服务消息
   * @param {*} data { touser:'openid of user', page, amount, title, remark, datetime, addr }
   * @param {*} cb 
   */
  push(data, cb) {
    return wx.cloud.callFunction({
      name: 'sendMsg',
      data
    }).then(res => {
      if (cb) cb(res)
      return res
    }).catch(err => {
      console.error(err)
      return err
    })
  },
  notifyDelivery(orderId, cb) {
    msgObj.notifyDeliveryMan(orderId, undefined, cb)
  },
  notifyDeliveryMan(orderId, deliveryManId, cb) {
    wx.cloud.callFunction({
      name: 'notifyDelivery',
      data: {
        orderId,
        deliveryManId
      }
    }).then(res => {
      if (cb) cb(res)
    }).catch(err => {
      console.error(err)
    })
  }
}
export const msg = msgObj